home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / OwnerDrawButtons / OwnerDrawButtons.cs next >
Encoding:
Text File  |  2001-01-15  |  4.7 KB  |  132 lines

  1. //-----------------------------------------------
  2. // OwnerDrawButtons.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8.  
  9. class OwnerDrawButtons: Form
  10. {
  11.      readonly int    cxImage, cyImage;
  12.      readonly int    cxBtn, cyBtn, dxBtn;
  13.      readonly Button btnLarger, btnSmaller;
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new OwnerDrawButtons());
  18.      }
  19.      public OwnerDrawButtons()
  20.      {
  21.           Text = "Owner-Draw Buttons";
  22.           ResizeRedraw = true;
  23.  
  24.           cxImage = 4 * Font.Height;
  25.           cyImage = 4 * Font.Height;
  26.           cxBtn = cxImage + 8;
  27.           cyBtn = cyImage + 8;
  28.           dxBtn = Font.Height;
  29.  
  30.           btnLarger = new Button();
  31.           btnLarger.Parent = this;
  32.           btnLarger.Size   = new Size(cxBtn, cyBtn);
  33.           btnLarger.Click += new EventHandler(ButtonLargerOnClick);
  34.           btnLarger.Paint += new PaintEventHandler(ButtonOnPaint);
  35.  
  36.           btnSmaller = new Button();
  37.           btnSmaller.Parent = this;
  38.           btnSmaller.Size   = new Size(cxBtn, cyBtn);
  39.           btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
  40.           btnSmaller.Paint += new PaintEventHandler(ButtonOnPaint);
  41.  
  42.          OnResize(EventArgs.Empty);
  43.      }
  44.      protected override void OnResize(EventArgs ea)
  45.      {
  46.           base.OnResize(ea);
  47.  
  48.           btnLarger.Location =
  49.                          new Point(ClientSize.Width / 2 - cxBtn - dxBtn / 2,
  50.                                   (ClientSize.Height - cyBtn) / 2);
  51.           btnSmaller.Location =
  52.                          new Point(ClientSize.Width / 2 + dxBtn / 2,
  53.                                   (ClientSize.Height - cyBtn) / 2);
  54.      }
  55.      void ButtonLargerOnClick(object obj, EventArgs ea)
  56.      {
  57.           Left   -= (int)(0.05 * Width);
  58.           Top    -= (int)(0.05 * Height);
  59.           Width  += (int)(0.10 * Width);
  60.           Height += (int)(0.10 * Height);
  61.      }
  62.      void ButtonSmallerOnClick(object obj, EventArgs ea)
  63.      {
  64.           Left   += (int)(Width  / 22f);
  65.           Top    += (int)(Height / 22f);
  66.           Width  -= (int)(Width  / 11f);
  67.           Height -= (int)(Height / 11f);
  68.      }
  69.      void ButtonOnPaint(object obj, PaintEventArgs pea)
  70.      {
  71.           Button   btn  = (Button) obj;
  72.           Graphics grfx = pea.Graphics; 
  73.  
  74.           ControlPaint.DrawButton(grfx, 0, 0, cxBtn, cyBtn, 
  75.                (btn == (Button) GetChildAtPoint(
  76.                                    PointToClient(
  77.                                         MousePosition))) && 
  78.                     btn.Capture ? ButtonState.Pushed : ButtonState.Normal);
  79.  
  80.           GraphicsState grfxstate = grfx.Save();
  81.  
  82.           grfx.TranslateTransform((cxBtn - cxImage) / 2, 
  83.                                   (cyBtn - cyImage) / 2);               
  84.           if (btn == btnLarger)
  85.                DrawLargerButton(grfx, cxImage, cyImage);
  86.           else
  87.                DrawSmallerButton(grfx, cxImage, cyImage);
  88.  
  89.           grfx.Restore(grfxstate);
  90.  
  91.           if (btn.Focused)
  92.                ControlPaint.DrawFocusRectangle(grfx, 
  93.                     new Rectangle((cxBtn - cxImage) / 2 + cxImage / 16,
  94.                                   (cyBtn - cyImage) / 2 + cyImage / 16,
  95.                                   7 * cxImage / 8, 7 * cyImage / 8));
  96.      }
  97.      void DrawLargerButton(Graphics grfx, int cx, int cy)
  98.      {
  99.           Brush brush = new SolidBrush(btnLarger.ForeColor);
  100.           Pen   pen   = new Pen(btnLarger.ForeColor);
  101.  
  102.           grfx.TranslateTransform(cx / 2, cy / 2);
  103.  
  104.           for (int i = 0; i < 4; i++)
  105.           {
  106.                grfx.DrawLine(pen, 0, 0, cx / 4, 0);
  107.                grfx.FillPolygon(brush, new Point[] {
  108.                                        new Point(cx / 4, -cy / 8),
  109.                                        new Point(cx / 2,       0),
  110.                                        new Point(cx / 4,  cy / 8)});
  111.                grfx.RotateTransform(90);
  112.           }
  113.      }     
  114.      void DrawSmallerButton(Graphics grfx, int cx, int cy)
  115.      {
  116.           Brush brush = new SolidBrush(btnSmaller.ForeColor);
  117.           Pen   pen   = new Pen(btnSmaller.ForeColor);
  118.  
  119.           grfx.TranslateTransform(cx / 2, cy / 2);
  120.  
  121.           for (int i = 0; i < 4; i++)
  122.           {
  123.                grfx.DrawLine(pen, 3 * cx / 8, 0, cx / 2, 0);
  124.                grfx.FillPolygon(brush, new Point[] {
  125.                                        new Point(3 * cx / 8, -cy / 8),
  126.                                        new Point(    cx / 8,       0),
  127.                                        new Point(3 * cx / 8,  cy / 8)});
  128.                grfx.RotateTransform(90);
  129.           }
  130.      }     
  131. }
  132.